import express, { Request, Response, NextFunction } from 'express'
import { addTag, deleteTag, queryAllTags } from '../dao/tagDao';
import { formatResponse } from '../utils/tool';

const router = express.Router()

router.post('/', async (req, res, next) => {
  const result = await addTag(req.body)
  if (result.affectedRows === 1) {
    res.send(formatResponse(0, "新增tag成功", null))
  } else {
    res.send(formatResponse(0, "新增tag失败", null))
  }
})

router.get('/', async (req, res, next) => {
  const result = await queryAllTags()
  res.send(formatResponse(0, '', result))
})

router.delete('/:id', async (req, res, next) => {
  const result = await deleteTag(+req.params.id)
  if (result.affectedRows === 1) {
    res.send(formatResponse(0, "删除tag成功", null))
  } else {
    res.send(formatResponse(0, "要删除的tag不存在", null))
  }
})

export default router